package goods;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AccountSystem {
    private static JFrame frame;
    private static JLabel remainingLabel;
    private static JLabel expenseLabel;
    private static JLabel daysLeftLabel;
    private static JLabel todayExpenseLabel;
    private double remainingAmount;
    private double totalExpense;
    private int daysLeft;
    private double todayExpense;

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                AccountSystem accountSystem = new AccountSystem();
                accountSystem.createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        // 创建主窗口
        frame = new JFrame("记账系统");
        frame.setBounds(480,300,500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // 创建组件
        remainingLabel = new JLabel("本月剩余：0");
        expenseLabel = new JLabel("本月消费：0");
        daysLeftLabel = new JLabel("距离本月末天数：30");
        todayExpenseLabel = new JLabel("今日消费金额：0");

        JButton recordButton = new JButton("记");
        recordButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                openSecondWindow();
            }
        });

        // 将组件添加到主窗口
        frame.add(remainingLabel, BorderLayout.EAST);
        frame.add(expenseLabel, BorderLayout.WEST);
        frame.add(daysLeftLabel, BorderLayout.SOUTH);
        frame.add(todayExpenseLabel, BorderLayout.NORTH);
        frame.add(recordButton, BorderLayout.CENTER);

        // 显示主窗口
        frame.pack();
        frame.setVisible(true);
    }

    private static void openSecondWindow() {
        // 创建第二个窗口
        JFrame secondFrame = new JFrame("记账");
        secondFrame.setBounds(450,300,500,500);
        secondFrame.setLayout(new GridLayout(3, 2));
        secondFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        // 创建组件
        JLabel incomeAmountLabel = new JLabel("收入金额：");
        JTextField incomeAmountField = new JTextField();
        JLabel incomeReasonLabel = new JLabel("收入原因：");
        JTextField incomeReasonField = new JTextField();
        JLabel expenseAmountLabel = new JLabel("支出金额：");
        JTextField expenseAmountField = new JTextField();
        JLabel expenseReasonLabel = new JLabel("支出原因：");
        JTextField expenseReasonField = new JTextField();

        JButton nextButton = new JButton("下一步");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 更新主窗口数据
                double incomeAmount = Double.parseDouble(incomeAmountField.getText());
                double expenseAmount = Double.parseDouble(expenseAmountField.getText());
                updateMainWindowData(incomeAmount, expenseAmount);

                // 打开第三个窗口
                openThirdWindow(incomeAmount, expenseAmount);

                // 关闭第二个窗口
                secondFrame.dispose();
            }
        });

        // 将组件添加到第二个窗口
        secondFrame.add(incomeAmountLabel);
        secondFrame.add(incomeAmountField);
        secondFrame.add(incomeReasonLabel);
        secondFrame.add(incomeReasonField);
        secondFrame.add(expenseAmountLabel);
        secondFrame.add(expenseAmountField);
        secondFrame.add(expenseReasonLabel);
        secondFrame.add(expenseReasonField);
        secondFrame.add(nextButton);

        // 显示第二个窗口
        secondFrame.pack();
        secondFrame.setVisible(true);
    }

    private static void openThirdWindow(double incomeAmount, double expenseAmount) {
        // 创建第三个窗口
        JFrame thirdFrame = new JFrame("收支明细");
        thirdFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        thirdFrame.setLayout(new BorderLayout());

        // 创建组件
        JTextArea detailsTextArea = new JTextArea();
        detailsTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(detailsTextArea);

        // 拼接收支明细内容
        StringBuilder detailsBuilder = new StringBuilder();
        detailsBuilder.append("日期\t\t收入\t\t支出\n");
        // 这里用示例数据代替实际的每日明细记录
        for (int i = 1; i <= 30; i++) {
            detailsBuilder.append(i + "日\t\t" + incomeAmount + "\t\t" + expenseAmount + "\n");
        }
        detailsTextArea.setText(detailsBuilder.toString());

        // 将组件添加到第三个窗口
        thirdFrame.add(scrollPane, BorderLayout.CENTER);

        // 显示第三个窗口
        thirdFrame.setSize(300, 400);
        thirdFrame.setVisible(true);
    }

    private static void updateMainWindowData(double incomeAmount, double expenseAmount) {
        // 获取当前主窗口数据
        double remaining = Double.parseDouble(remainingLabel.getText().substring(5));
        double expense = Double.parseDouble(expenseLabel.getText().substring(5));
        int daysLeft = Integer.parseInt(daysLeftLabel.getText().substring(8));
        double todayExpense = Double.parseDouble(todayExpenseLabel.getText().substring(7));

        // 更新数据
        remaining += incomeAmount - expenseAmount;
        expense += expenseAmount;
        daysLeft--;
        todayExpense += expenseAmount;

        // 更新主窗口显示
        remainingLabel.setText("本月剩余：" + remaining);
        expenseLabel.setText("本月消费：" + expense);
        daysLeftLabel.setText("距离本月末天数：" + daysLeft);
        todayExpenseLabel.setText("今日消费金额：" + todayExpense);
    }

    private void recordTransaction(double incomeAmount, double expenseAmount) {
        remainingAmount += incomeAmount - expenseAmount;
        totalExpense += expenseAmount;
        daysLeft--;
        todayExpense += expenseAmount;
    }

    private void updateMainWindow() {
        remainingLabel.setText("本月剩余：" + remainingAmount);
        expenseLabel.setText("本月消费：" + totalExpense);
        daysLeftLabel.setText("距离本月末天数：" + daysLeft);
        todayExpenseLabel.setText("今日消费金额：" + todayExpense);
    }
}

